package com.tekj.cxqcb.view.Module.activity.StoreModule;

import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.operation.resultBean.QueryHotWordBean;
import com.tekj.cxqcb.view.Module.adapter.HotWordsAdapter;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

import static android.widget.ListPopupWindow.MATCH_PARENT;

/**
 * 热词列表
 */
public class HotWordsActivity extends BaseActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.srv_main)
    SwipeRecyclerView srvMain;
    @BindView(R.id.tv_add)
    TextView tvAdd;

    BusinessDao businessDao;
    HotWordsAdapter hotWordsAdapter;

    @Override
    public void init() {
        tvTitle.setText("热词配置");
        businessDao = new BusinessDao(activity, activity);


        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
//                SwipeMenuItem deleteItem1 = new SwipeMenuItem(activity)
//                        .setHeight(MATCH_PARENT)
//                        .setWidth(Tools.dip2Px(activity, 60))
//                        .setBackgroundColor(getResources().getColor(R.color.colorPrimary))
//                        .setText("编辑").setTextSize(13)
//                        .setTextColor(getResources().getColor(R.color.white));
//                rightMenu.addMenuItem(deleteItem1);

                SwipeMenuItem deleteItem2 = new SwipeMenuItem(activity)
                        .setHeight(MATCH_PARENT)
                        .setWidth(Tools.dip2Px(activity, 60))
                        .setBackgroundColor(getResources().getColor(R.color.red))
                        .setText("删除").setTextSize(13)
                        .setTextColor(getResources().getColor(R.color.white));

                rightMenu.addMenuItem(deleteItem2);
                // 注意：哪边不想要菜单，那么不要添加即可。
            }
        };

        srvMain.setSwipeMenuCreator(mSwipeMenuCreator);

        // 设置监听器。
        // 菜单点击监听。
        OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, final int position) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();

                // 左侧还是右侧菜单：
                int direction = menuBridge.getDirection();
                if (direction == -1) {
                    // 菜单在Item中的Position：
                    switch (menuBridge.getPosition()) {
//                        case 0:
//                            showPop(classificationAdapter.getData().get(position).getTypeName(),
//                                    classificationAdapter.getData().get(position).getId());
//                            break;
                        case 0:
                            Tools.showNormalDialogOne(activity, "删除", "是否将该热词删除"
                                    , new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            businessDao.DelHotWord(hotWordsAdapter.getData().get(position).getId());
                                        }
                                    }, null);
                            break;
                    }

                }
            }
        };
        srvMain.setOnItemMenuClickListener(mItemMenuClickListener);

        hotWordsAdapter = new HotWordsAdapter(null);
        srvMain.setLayoutManager(new LinearLayoutManager(activity));
        srvMain.setAdapter(hotWordsAdapter);
        srvMain.addItemDecoration(new SpacesItemDecoration(1));

        businessDao.QueryHotWord(MainBActivity.User.getShopId());
    }

    @Override
    public int setLayout() {
        return R.layout.activity_hot_words_list;
    }


    boolean isF = true;

    @Override
    public void initLoadStart() {
        if (isF) {
            loading.show();
            isF = false;
        } else {
            showDialog();
        }
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 热词配置查询:
                QueryHotWordBean queryHotWordBean = (QueryHotWordBean) bindingViewBean.getBean();
                if (queryHotWordBean.getCode().equals("0000000")) {
                    hotWordsAdapter.setNewData(queryHotWordBean.getData());
                }
                break;
            case 删除热词:
                SuccessfulRevision(bindingViewBean);
                break;
            case 新增热词:
                SuccessfulRevision(bindingViewBean);
                break;
        }

    }

    void SuccessfulRevision(BindingViewBean bindingViewBean) {
        PubStatusBean bean = (PubStatusBean) bindingViewBean.getBean();
        if (bean.getCode().equals("0000000")) {
            if (popupWindow != null)
                popupWindow.dismiss();
            businessDao.QueryHotWord(MainBActivity.User.getShopId());
        }
        Toasty.normal(activity, bean.getMsg()).show();
    }

    @OnClick({R.id.ll_back, R.id.ll_right, R.id.tv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_right:
                break;
            case R.id.tv_add:
                showPop();
                break;
        }
    }

    private PopupWindow popupWindow;
    private View popupView;
    private TranslateAnimation animation;

    void initPop() {
        Tools.lightoff(activity);
        popupView = View.inflate(activity, R.layout.pop_add_classification, null);
        popupWindow = new PopupWindow(popupView, width - Tools.dip2Px(activity, 40),
                LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Tools.lighton(activity);
            }
        });
    }

    private void showPop() {
        initPop();
        TextView tvTitle = popupView.findViewById(R.id.tv_title);
        tvTitle.setText("新增热词");
        TextView tvQx = popupView.findViewById(R.id.tv_qx);
        TextView tvQr = popupView.findViewById(R.id.tv_qr);
        final EditText etClassName = popupView.findViewById(R.id.et_class_name);
        etClassName.setHint("请填写热词");
        tvQx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        tvQr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etClassName.getText().toString().trim().equals("")) {
                    Toasty.normal(activity, "请先填写热词").show();
                    return;
                }
                businessDao.AddHotWord(MainBActivity.User.getShopId(), etClassName.getText().toString().trim());
            }
        });

        popupWindow.showAtLocation(rlTitleBg, Gravity.CENTER, 0, 0);
        popupView.startAnimation(animation);

    }
}
